<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    okText="下载代码"
    width="1000px"
  >
    <BasicForm @register="registerForm" />
    <a-collapse v-model:activeKey="activeKey" accordion>
      <a-collapse-panel key="1" header="后端代码路径">
        <a-list size="small" bordered :data-source="javaPathList">
          <template #renderItem="{ item }">
            <a-list-item>{{ item }}</a-list-item>
          </template>
        </a-list>
      </a-collapse-panel>
      <a-collapse-panel key="2" header="前端代码路径" :disabled="false">
        <a-list size="small" bordered :data-source="vuePathList">
          <template #renderItem="{ item }">
            <a-list-item>{{ item }}</a-list-item>
          </template>
        </a-list>
      </a-collapse-panel>
    </a-collapse>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, onUnmounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { codeGenDowformSchema } from './generateFrom.data';
  import { downloadCodeZip } from '/@/api/generate/generate.api';
  import { downloadByData } from '/@/utils/file/download';

  export default defineComponent({
    name: 'DeptModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      let sortable1: any;
      const javaPathList = ref([]);
      const vuePathList = ref([]);
      let initTime: any;
      const activeKey = ref([]);

      onUnmounted(() => {
        clearTimeout(initTime);
        if (sortable1) {
          sortable1.destroy();
        }
      });

      const isUpdate = ref(true);

      const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
        labelWidth: 100,
        baseColProps: { span: 22 },
        schemas: codeGenDowformSchema,
        showActionButtonGroup: false,
        compact: true,
      });

      const [registerModal, { setModalProps, closeModal, changeOkLoading }] = useModalInner(
        async (data) => {
          resetFields();
          setModalProps({ confirmLoading: false });
          isUpdate.value = !!data?.isUpdate;
          setFieldsValue({
            ...data.res,
          });
          javaPathList.value = data.res.javaPathList;
          vuePathList.value = data.res.vuePathList;
        },
      );

      const getTitle = computed(() => '代码生成成功');

      async function handleSubmit() {
        try {
          changeOkLoading(true);
          const values = await validate();
          await downloadCodeZip(values).then((res) => {
            downloadByData(res.data, 'code.zip', 'application/octet-stream');
          });
          // closeModal();
          // emit('success');
        } finally {
          changeOkLoading(false);
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        vuePathList,
        javaPathList,
        activeKey,
        registerModal,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
